<template>
  <div class="container">
    <el-card class="box-card">
      <el-form :inline="true" :model="formInline" class="demo-form-inline">
        <el-form-item class="school" label="学科名称">
          <el-input v-model="formInline.user" placeholder=""></el-input>
        </el-form-item>
        <el-button size="small" class="eliminate" @click="formInline.user = ''"
          >清除</el-button
        >
        <el-button type="primary" size="small" @click="souso">搜索</el-button>
        <el-button
          style="float: right; "
          type="success"
          icon="el-icon-edit"
          size="small"
          @click="dialogVisible = true"
          >新增学科</el-button
        >
      </el-form>
      <el-alert
        class="alert"
        :title="alertText"
        type="info"
        show-icon
        :closable="false"
      ></el-alert>
      <el-table :data="tableData" style="width: 100%">
        <el-table-column type="index" label="序号" width="80">
        </el-table-column>
        <el-table-column prop="subjectName" label="学科名称" width="180">
        </el-table-column>
        <el-table-column prop="username" label="创建者" width="180">
        </el-table-column>
        <el-table-column prop="addDate" label="创建日期" width="280">
        </el-table-column>
        <el-table-column prop="isFrontDisplay" label="前台是否显示" width="180">
          <template slot-scope="scope">
            <span v-if="scope.row.isFrontDisplay == 1">是</span>
            <span v-else>否</span>
          </template>
        </el-table-column>
        <el-table-column prop="twoLevelDirectory" label="二级目录" width="180">
        </el-table-column>
        <el-table-column prop="tags" label="标签" width="180"> </el-table-column>
        <el-table-column prop="totals" label="题目数量" width="180">
        </el-table-column>
        <el-table-column prop="operation " label="操作" width="230">
          <template slot-scope="scope">
            <el-button
              type="text"
              size="small"
              @click="$router.push('/subjects/directorys')"
              >学科分类</el-button
            >
            <el-button
              type="text"
              size="small"
              @click="$router.push('/subjects/tags')"
              >学科标签
            </el-button>
            <el-button type="text" size="small" @click="xiugai(scope.row)"
              >修改</el-button
            >
            <el-button type="text" size="small" @click="removeById(scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <div class="block">
        <el-pagination
          background
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="qurey.page"
          :page-sizes="[5, 10, 15, 20]"
          :page-size="qurey.pagesize"
          class="suibian"
          layout="total, prev, pager, next, sizes, jumper"
          :total="total"
        >
        </el-pagination>
      </div>
    </el-card>
    <el-form
      :model="ruleForm"
      :rules="rules"
      ref="ruleForm"
      label-width="100px"
      class="demo-ruleForm"
    >
      <el-dialog
        @close="ruleForm.name = ''"
        title="新增学科"
        :visible.sync="dialogVisible"
        width="30%"
      >
        <el-form-item label="学科名称" prop="name">
          <el-input
            v-model="ruleForm.name"
            placeholder="请输入学科名称"
          ></el-input>
        </el-form-item>
        <el-form-item label="是否显示">
          <el-switch
            v-model="value"
            active-color="#13ce66"
            inactive-color="#ff4949"
          >
          </el-switch>
        </el-form-item>
        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="sure">确 定</el-button>
        </span>
      </el-dialog>
    </el-form>
    <el-form
      :model="ruleForm"
      :rules="rules"
      ref="ruleForm"
      label-width="100px"
      class="demo-ruleForm"
    >
      <el-dialog
        title="修改学科"
        :visible.sync="xiugaidialogVisible"
        width="30%"
      >
        <el-form-item label="学科名称" prop="name">
          <el-input
            v-model="ruleForm.subjectName"
            placeholder="请输入学科名称"
          ></el-input>
        </el-form-item>
        <el-form-item label="是否显示">
          <el-switch
            v-model="value"
            active-color="#13ce66"
            inactive-color="#ff4949"
          >
          </el-switch>
        </el-form-item>
        <span slot="footer" class="dialog-footer">
          <el-button @click="xiugaidialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="xiugaisure">确 定</el-button>
        </span>
      </el-dialog>
    </el-form>
  </div>
</template>

<script>
import { list, remove, add, update } from "@/api/hmmm/subjects";
export default {
  data() {
    return {
      total: 0,
      alertText: "",
      formInline: {
        user: "",
        region: ""
      },
      tableData: [],
      qurey: {
        page: 1,
        pagesize: 10
      },
      dialogVisible: false,
      xiugaidialogVisible: false,
      ruleForm: {
        subjectName: ""
      },
      rules: {
        subjectName: [
          { required: true, message: "请输入学科名称", trigger: "blur" },
          { min: 1, max: 25, message: "长度在 1 到 25 个字符", trigger: "blur" }
        ]
      },
      value: true
    };
  },
  methods: {
    xiugai(row) {
      this.xiugaidialogVisible = true;
      this.ruleForm.subjectName = row.subjectName;
      this.ruleForm.id = row.id;
    },
    async xiugaisure() {
      try {
        const query = this.ruleForm;
        console.log(query);
        const { data } = await update({
          id: query.id,
          subjectName: query.subjectName,
          isFrontDisplay: 1
        });
        console.log(data);
        this.$message.success("修改成功");
      } catch (err) {
        console.log(err);
      }
      this.gitList();
      this.xiugaidialogVisible = false;
    },
    async sure() {
      try {
        console.log(123);
        await add({ subjectName: this.ruleForm.name, isFrontDisplay: 1 });
      } catch (err) {
        console.log(err);
      }
      this.gitList();
      this.dialogVisible = false;
    },
    async souso() {
      try {
        const { data } = await list({
          ...this.qurey,
          subjectName: this.formInline.user
        });
        console.log(data);
        this.tableData = data.items;
      } catch (err) {
        console.log(err);
      }
    },
    onSubmit() {
      console.log("submit!");
    },
    async gitList() {
      try {
        const { data } = await list(this.qurey);
        console.log(data);
        this.tableData = data.items;
        this.total = data.counts;
        this.alertText = `数据一共是 ${this.total} 条`;
      } catch (err) {
        console.log(err);
      }
    },
    handleClick(row) {
      console.log(row);
    },
    handleSizeChange(val) {
      this.qurey.pagesize = val;
      this.gitList(this.qurey);
    },
    handleCurrentChange(val) {
      this.qurey.page = val;
      this.gitList(this.qurey);
    },
    async removeById(row) {
      console.log(row);
      const confirmResult = await this.$confirm(
        "此操作将永久删除该标签, 是否继续?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }
      ).catch(err => err);
      if (confirmResult !== "confirm") {
        return this.$message.info("已经取消删除！");
      }
      const { data: res } = await remove(row);
      console.log(res);
      this.$message.success("删除成功！");
      this.gitList();
    }
  },
  created() {
    this.gitList();
  }
};
</script>

<style scoped>
.school {
  margin-left: 12px;
}
.el-alert {
  margin-bottom: 10px;
}
.suibian {
  display: flex;
  justify-content: flex-end;
}
.block {
  margin-top: 20px;
}
.dialog-footer {
  display: flex;
  justify-content: flex-end;
}
</style>
